package com.vincent.jvmtest.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author vincent
 * @date 2019/3/3
 * @description
 */
public class DelayedQueueDemo {
    public static Logger logger = LoggerFactory.getLogger(DelayedQueueDemo.class);
    public static DelayQueue<DelayedVo<String>> queue = new DelayQueue<>();
    public static Map<String, String> map = new ConcurrentHashMap<>(128);
    public static void main(String[] args) {
        logger.info("----------------- start up ----------------");
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(() -> {
            while (true) {
                try {
                    DelayedVo<String> take = queue.take();
                    String key = take.getValue();
                    logger.info("remove by key = {}", key);
                    map.remove(key);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });
        map.put("k1", "v1");
        queue.put(new DelayedVo<>(10 * 1000, "k1"));
        logger.info("add cache");
    }
}
